################################################################################
##############00.Master File####################################################
####This file calls the other files necessary to replicate all results##########
######################Jacob R. Gunderson########################################
#####################gundejac@live.unc.edu######################################
################################################################################

#Install necessary Packages#

packages <- c('readr', 'readxl', 'dplyr', 'SDMTools', 'rio', 'manifestoR', 
              'imputeTS', 'schoolmath', 'lme4', 'MASS', 'plyr', 'plm', 'psce', 
              'lmtest', 'ggplot2', 'ggrepel', 'MuMIn', 'ggcorrplot', 'interplot',
              'reshape2', 'interflex', 'gridExtra', 'xtable')

#install.packages(packages, dependencies = T)

#####Set Seed for replicability#####

set.seed(121212)

#####Set Working Directory#####

#setwd("")

#Set working directory to the file where the data are stored#

#####Read in and Clean CHES Data#####

#After this project was completed, the CHES team produced a new trend file including 
#Survway waves after 2014. I have maintained this code as it was used at the time of 
#manuscript submission#

#source("01.CHES_prep.R")

#correlations reported in footnote 31#

#cor.test(CHES$lrecon, CHES$redistribution) #0.946 [0.937, 0.954] p < 2.2e-16
#cor.test(CHES$lrecon, CHES$spendvtax) #0.942 [0.933, 0.950] p < 2.2e-16
#cor.test(CHES$lrecon, CHES$deregulation) #0.947 [0.938, 0.954] p < 2.2e-16
#cor.test(CHES$lrecon, CHES$econ_interven) #0.959 [0.948, 0.968] p <2.2e-16

#####Read in, clean, calcualte, and merge salience, inequality and other controls#####

#source("02.Salience_and_controls.R")

#####Calculate Horizontal Inequality (Partisan Sorting Measure)################

#This will take a little bit of time.#

#source("03.Horizontal_inequality.R")

#####Make final changes to data (scale and pdata format) and Save data#########

#source("04.Final_data_adjustments_and_saving.R")

#This produces the file save as pols_bjps and referred to in the text as pols.

##NOTE: Once steps 1-4 have been run once, they do not need to be run again####

######Create Figure One and Two: Demonstrating Polarization#####################

source("05.Figure_1_2.R") #Saves figure to working directory#

#output: figure_1.pdf
#        figure_2.pdf

######Table 1: Fixed Effects Models#############################################

source("06.Table1.R") #Runs fixed effects models for Table 1
#Due to averaging of models together, output printed to console and put in latex
#by hand. 
#Also includes tests for importance/necessity of fixed effects#

#output: Values for Table 1
#        Values for Table A11

#####Figures 2 and 3###########################################################

#Must be run immediately after 06.Table1.R"#

#It will take some time#

source("07.Figures_3_4.R")

#Output: figure_3_fe.pdf
#        figure_4_fe.pdf

#########Table 2################################################################

source("08.Table_2.R")

#Output: Values for Table 2

###################Figure 5#####################################################

source("09.Figure_5.R")

#Output: figure_5.pdf

#######################Supplementary Material###################################

##################Salience Categories and Summary Statistics####################

source("10.sum_stat.R")
#Prints summary statistics to console

#Output: Table A3, A4, A5, and A7

#*For Table A6 see between_within_sd.do

##################Relationship between salience and polarization################

source("11.Figure_A1.R")

#Output: Figure_A1.pdf

###################Table A8: Models using only CSES Data#########################

source("12.CSES_only.R")

#Ouput: TAble A8

###################Table A9: Fixed Effects with Alternative Income Diff.########

source("13.Table_10_fe_alternate_inc_dif.R")

#Output: Table A9

#################Robustness Check Dropping Every Country########################

source("14.robustness_drop.R")

#Output: figure_A2.pdf

###################10. Multilevel models and Marginal Effects###################

source("15.MLM_marginaleffects.R")

#Output: Table A10
#        figure_A3.pdf
#        figure_A4.pdf

##################16. Robustness check with WIID################################

source('16. Robustness Check with WIID.R')

#Output: Table A12
#        figure_A5.pdf
#        figure_A6.pdf

################Table A13: West European Data Only###############################

source("17.West_europe_data.R")

#Output: Table A13
#        figure_A7.pdf
#        figure_A8.pdf

###################Alternative Economic Controls Test###########################

source("18.Robustness_alt_econ.R")

#Output: Table A14

##################Figure A9: FE with single interactions########################

source("19.fe_single_int.R")

#Output: figure_A9.pdf

#################Figure A10: MLM with single interactions#########################

source("20.mlm_single_int.R")

#Output: figure_A10.pdf

################################################################################
################END OF REPLICATION CODE#########################################
################################################################################